let arr = [1,[1,2,[1,[2]]],[1],[3,[2]]]


Array.prototype.myflat = function(num = 0) {
    let arr = this
    let i = 0
    while(arr.some(item => Array.isArray(item))) {
        arr = [].concat(...arr)
        i++
        if(i > num) break
    }
    return arr
}
console.log(arr.myflat(5));
